package com.nhjf.mobile.controller.courses;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nhjf.common.page.Pagination;
import com.nhjf.mobile.common.util.SysProperties;
import com.nhjf.model.coursesSeries.CoursesSeries;
import com.nhjf.model.student.Student;
import com.nhjf.model.system.AdminSession;
import com.nhjf.model.system.User;
import com.nhjf.model.teachers.OrganizationHasStudent;
import com.nhjf.model.teachers.Teacher;
import com.nhjf.pojo.qo.coursesSeries.CoursesSeriesQo;
import com.nhjf.pojo.qo.studnet.StudentQo;
import com.nhjf.service.coursesSeries.CoursesSeriesService;
import com.nhjf.service.student.StudentService;
import com.nhjf.service.system.AdminSessionService;
import com.nhjf.service.system.UserService;
import com.nhjf.service.teachers.OrganizationHasStudentService;
import com.nhjf.service.teachers.TeachersService;

import net.sf.json.JSONObject;

import com.nhjf.pojo.qo.system.AdminSessionQo;
import com.nhjf.pojo.qo.system.UserQo;
import com.nhjf.pojo.qo.teachers.OrganizationHasStudentQo;
import com.nhjf.pojo.qo.teachers.TeachersQo;

@RequestMapping("/institutional")
@Controller
public class InstitutionalController {
	@Autowired
	private AdminSessionService adminSessionService;
	@Autowired
	private UserService userService;
	@Autowired
	private CoursesSeriesService seriesService;
	@Autowired
	private StudentService studentService;
	@Autowired
	private OrganizationHasStudentService osdService;
	@Autowired
	private TeachersService teacherService;
	/**
	 * 首页课程展示
	 * @param token
	 * @return
	 */
	@RequestMapping("/institutional_Page")
	@ResponseBody
	public String institutional_Page(@RequestParam(value="token", required=false) String token) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		try {
			AdminSessionQo adminSessionQo=new AdminSessionQo();
			adminSessionQo.setToken(token);
			AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
			if(adminSession!=null) {
				UserQo userQo=new UserQo();
				userQo.setUserName(adminSession.getLoginName());
				User user= userService.queryUnique(userQo);
				if(user!=null) {
					if(user.getIsRole()==2) {
						StudentQo qo= new StudentQo();
						qo.setSeachStudent(true);
						qo.setUserid(user.getId());
						Student student=studentService.queryUnique(qo);
						OrganizationHasStudentQo hasStudentQo=new OrganizationHasStudentQo();
						hasStudentQo.setStudentId(student.getId());
						OrganizationHasStudent hasStudent= osdService.queryUnique(hasStudentQo);
						if(hasStudent!=null) {
							CoursesSeriesQo coursesSeriesQo=new CoursesSeriesQo();
							coursesSeriesQo.setSeachTeacher(true);
							coursesSeriesQo.setOrganizationId(hasStudent.getOrganization().getId());
							Pagination pagination = new Pagination();
							pagination.setPageSize(2);
							pagination.setCondition(coursesSeriesQo);
							pagination= seriesService.queryPagination(pagination);
							List<CoursesSeries> series=(List<CoursesSeries>) pagination.getList();
							SysProperties sysProperties=SysProperties.getInstance();
							String imagePath=sysProperties.get("imagePath");
							for (CoursesSeries coursesSeries : series) {
								TeachersQo teachersQo=new TeachersQo();
								teachersQo.setTeacherId(coursesSeries.getUploader().getId());
								teachersQo.setSeachUser(true);
								Teacher teacher=teacherService.queryUnique(teachersQo);
								coursesSeries.setUserId(teacher.getUser().getId());
								if(StringUtils.isNotEmpty(coursesSeries.getUploader().getAvatar())) {
									if(coursesSeries.getUploader().getAvatar().indexOf(imagePath+"/picture/")>=0) {
									}else {
										coursesSeries.getUploader().setAvatar(imagePath+"/picture/"+coursesSeries.getUploader().getAvatar());
									}
								}
								coursesSeries.setCover(imagePath+"/img/"+coursesSeries.getCover());
								coursesSeries.setTotalDetails("");
								coursesSeries.setCourseTypeOne(null);
								coursesSeries.setCourseTypeTwo(null);
							}
							pagination.setList(series);
							resMap.put("code", "200");
							resMap.put("message", pagination);
							return JSONObject.fromObject(resMap).toString();
						}else {
							resMap.put("code", "500");
							resMap.put("message", "你没有所属于的机构！");
							return JSONObject.fromObject(resMap).toString();
						}
					}else {
						TeachersQo qo=new TeachersQo();
						qo.setUserId(user.getId());
						qo.setSeachOrganization(true);
						qo.setSeachUser(true);
						Teacher teacher1=teacherService.queryUnique(qo);
						if(teacher1!=null) {
							Pagination pagination = new Pagination();
							pagination.setPageSize(2);
							CoursesSeriesQo coursesSeriesQo=new CoursesSeriesQo();
							coursesSeriesQo.setSeachTeacher(true);
							coursesSeriesQo.setOrganizationId(teacher1.getTraininginstitution().getId());//所属机构id
							pagination.setCondition(coursesSeriesQo);
							pagination = seriesService.queryPagination(pagination);
							List<CoursesSeries> CoursesSeries=(List<CoursesSeries>)pagination.getList();
							SysProperties sysProperties=SysProperties.getInstance();
							String imagePath=sysProperties.get("imagePath");
							for (CoursesSeries coursesSeries2 : CoursesSeries) {
								TeachersQo teachersQo=new TeachersQo();
								teachersQo.setTeacherId(coursesSeries2.getUploader().getId());
								teachersQo.setSeachUser(true);
								Teacher teacher=teacherService.queryUnique(teachersQo);
								coursesSeries2.setUserId(teacher.getUser().getId());
								if(StringUtils.isNotEmpty(coursesSeries2.getUploader().getAvatar())) {
									if(coursesSeries2.getUploader().getAvatar().indexOf(imagePath+"/picture/")>=0) {
									}else {
										coursesSeries2.getUploader().setAvatar(imagePath+"/picture/"+coursesSeries2.getUploader().getAvatar());
									}
								}
								coursesSeries2.setCover(imagePath+"/img/"+coursesSeries2.getCover());
								coursesSeries2.setTotalDetails("");
								coursesSeries2.setCourseTypeOne(null);
								coursesSeries2.setCourseTypeTwo(null);
							}
							pagination.setList(CoursesSeries);
							resMap.put("code", "200");
							resMap.put("message", pagination);
							return JSONObject.fromObject(resMap).toString();
						}
					}
				}else {
					resMap.put("code", "500");
					resMap.put("message", "请先登录!");
					return JSONObject.fromObject(resMap).toString();
				}
			}
			resMap.put("code", "500");
			resMap.put("message", "操作异常!");
			return JSONObject.fromObject(resMap).toString();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resMap.put("code", "500");
			resMap.put("message", "异常抛出!");
			return JSONObject.fromObject(resMap).toString();
		}
	}
	/**
	 * 课程列表
	 * @param page
	 * @param token
	 * @return
	 */
	@RequestMapping("/institutional_List")
	@ResponseBody
	public String institutional_List(@RequestParam(value="page", defaultValue="1")Integer page,@RequestParam(value="token", required=false) String token) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		try {
			AdminSessionQo adminSessionQo=new AdminSessionQo();
			adminSessionQo.setToken(token);
			AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
			if(adminSession!=null) {
				UserQo userQo=new UserQo();
				userQo.setUserName(adminSession.getLoginName());
				User user= userService.queryUnique(userQo);
				if(user!=null) {
					if(user.getIsRole()==2) {
						StudentQo qo= new StudentQo();
						qo.setSeachStudent(true);
						qo.setUserid(user.getId());
						Student student=studentService.queryUnique(qo);
						OrganizationHasStudentQo hasStudentQo=new OrganizationHasStudentQo();
						hasStudentQo.setStudentId(student.getId());
						OrganizationHasStudent hasStudent= osdService.queryUnique(hasStudentQo);
						if(hasStudent!=null) {
							CoursesSeriesQo coursesSeriesQo=new CoursesSeriesQo();
							coursesSeriesQo.setSeachTeacher(true);
							coursesSeriesQo.setOrganizationId(hasStudent.getOrganization().getId());
							Pagination pagination = new Pagination();
							pagination.setPageSize(10);
							pagination.setPageNo(page);
							pagination.setCondition(coursesSeriesQo);
							pagination= seriesService.queryPagination(pagination);
							List<CoursesSeries> series=(List<CoursesSeries>) pagination.getList();
							SysProperties sysProperties=SysProperties.getInstance();
							String imagePath=sysProperties.get("imagePath");
							for (CoursesSeries coursesSeries : series) {
								TeachersQo teachersQo=new TeachersQo();
								teachersQo.setTeacherId(coursesSeries.getUploader().getId());
								teachersQo.setSeachUser(true);
								Teacher teacher=teacherService.queryUnique(teachersQo);
								coursesSeries.setUserId(teacher.getUser().getId());
								if(StringUtils.isNotEmpty(coursesSeries.getUploader().getAvatar())) {
									if(coursesSeries.getUploader().getAvatar().indexOf(imagePath+"/picture/")>=0) {
									}else {
										coursesSeries.getUploader().setAvatar(imagePath+"/picture/"+coursesSeries.getUploader().getAvatar());
									}
								}
								coursesSeries.setCover(imagePath+"/img/"+coursesSeries.getCover());
								coursesSeries.setTotalDetails("");
								coursesSeries.setCourseTypeOne(null);
								coursesSeries.setCourseTypeTwo(null);
							}
							pagination.setList(series);
							resMap.put("code", "200");
							resMap.put("message", pagination);
							return JSONObject.fromObject(resMap).toString();
						}else {
							resMap.put("code", "500");
							resMap.put("message", "你没有所属于的机构！");
							return JSONObject.fromObject(resMap).toString();
						}
					}else {
						TeachersQo qo=new TeachersQo();
						qo.setUserId(user.getId());
						qo.setSeachOrganization(true);
						qo.setSeachUser(true);
						Teacher teacher1=teacherService.queryUnique(qo);
						if(teacher1!=null) {
							Pagination pagination = new Pagination();
							pagination.setPageSize(10);
							CoursesSeriesQo coursesSeriesQo=new CoursesSeriesQo();
							coursesSeriesQo.setSeachTeacher(true);
							coursesSeriesQo.setOrganizationId(teacher1.getTraininginstitution().getId());//所属机构id
							pagination.setCondition(coursesSeriesQo);
							pagination.setPageNo(page);
							pagination = seriesService.queryPagination(pagination);
							List<CoursesSeries> CoursesSeries=(List<CoursesSeries>)pagination.getList();
							SysProperties sysProperties=SysProperties.getInstance();
							String imagePath=sysProperties.get("imagePath");
							for (CoursesSeries coursesSeries2 : CoursesSeries) {
								TeachersQo teachersQo=new TeachersQo();
								teachersQo.setTeacherId(coursesSeries2.getUploader().getId());
								teachersQo.setSeachUser(true);
								Teacher teacher=teacherService.queryUnique(teachersQo);
								coursesSeries2.setUserId(teacher.getUser().getId());
								if(StringUtils.isNotEmpty(coursesSeries2.getUploader().getAvatar())) {
									if(coursesSeries2.getUploader().getAvatar().indexOf(imagePath+"/picture/")>=0) {
									}else {
										coursesSeries2.getUploader().setAvatar(imagePath+"/picture/"+coursesSeries2.getUploader().getAvatar());
									}
								}
								coursesSeries2.setCover(imagePath+"/img/"+coursesSeries2.getCover());
								coursesSeries2.setTotalDetails("");
								coursesSeries2.setCourseTypeOne(null);
								coursesSeries2.setCourseTypeTwo(null);
							}
							pagination.setList(CoursesSeries);
							resMap.put("code", "200");
							resMap.put("message", pagination);
							return JSONObject.fromObject(resMap).toString();
						}
					}
				}else {
					resMap.put("code", "500");
					resMap.put("message", "请先登录!");
					return JSONObject.fromObject(resMap).toString();
				}
			}
			resMap.put("code", "500");
			resMap.put("message", "操作异常!");
			return JSONObject.fromObject(resMap).toString();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resMap.put("code", "500");
			resMap.put("message", "异常抛出!");
			return JSONObject.fromObject(resMap).toString();
		}


	}
}
